package com.fbinggun.fbinggunutil.fgive.command.args;

import com.fbinggun.fbinggunutil.FBinggunUtil;
import com.fbinggun.fbinggunutil.command.ACommand;
import com.fbinggun.fbinggunutil.command.MainCommand;
import com.fbinggun.fbinggunutil.fgive.FgiveAPI;
import com.fbinggun.fbinggunutil.fgive.Util;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;

import java.io.IOException;

public class Save extends ACommand {
    FgiveAPI api;
    public Save(MainCommand mainCommand) {
        super(mainCommand);
        api= FBinggunUtil.plugin.getFgiveAPI();
    }

    @Override
    public String getPresentation() {
        return "&e/fgive save <物品名> &7将手中物品保存至物品库中";
    }

    @Override
    public boolean isOp() {
        return true;
    }

    @Override
    public String getPermission() {
        return "";
    }

    @Override
    public String getId() {
        return "save";
    }

    @Override
    public boolean run(CommandSender sender, String[] args) {
                Player player= (Player) sender;
                String name= args[1];
                if(name==null){
                    sendMessage(sender,"&c请输入你要保存的名字");
                    return false;
                }
                ItemStack itemStack =player.getInventory().getItemInMainHand();
                if(itemStack!=null){
                    itemStack=itemStack.clone();
                    if(api.getItemStack(name)!=null){
                        if(args.length<=3){
                            sendMessage(sender,"&c物品已存在 如需替换请输入/fgive save "+name+" t");
                            return false;
                        }else {
                            if(!args[2].equalsIgnoreCase("t")){
                                sendMessage(sender,"&c物品已存在 如需替换请输入/fgive save "+name+" t");
                                return false;
                            }
                        }
                    }
                    itemStack.setAmount(1);
                    api.save(args[1],itemStack);
                    sender.sendMessage("§a保存物品成功");
                }else {
                    sender.sendMessage("§c要手持一个物品");
                    return false;
                }
        return false;
    }

    @Override
    public boolean isPlayer() {
        return true;
    }
}
